package com.halixun.d2app.adapter.holder;

import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import com.halixun.d2app.R;
import com.halixun.d2app.adapter.PrintListAdapter;
import com.halixun.d2app.base.BasePrintInfo;
import com.halixun.d2app.base.BaseViewHolder;
import com.halixun.d2app.bean.print.BlueToothPrintInfo;
import com.halixun.d2app.bean.print.NetPrintInfo;
import com.halixun.d2app.bean.print.UsbPrintInfo;
import com.halixun.d2app.utils.DensityUtil;

/**
 * @ProjectName: D2App
 * @Package: com.halixun.d2app.adapter.holder.print
 * @ClassName: UsbPrintViewHolder
 * @Description: java类作用描述
 * @Author: hdx
 * @CreateDate: 2019/12/8 14:50
 * @UpdateUser: hdx
 * @UpdateDate: 2019/12/8 14:50
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class OtherPrintViewHolder extends BaseViewHolder<BasePrintInfo> {

    private NetPrintInfo mNetPrintInfo;

    private BlueToothPrintInfo mBlueToothPrintInfo;

    private TextView ip;

    private PrintListAdapter.PrintListListener mPrintListListener;


    public OtherPrintViewHolder(View itemView) {
        super(itemView);
        initView();
        setListeners();
    }

    public OtherPrintViewHolder(View itemView, PrintListAdapter.PrintListListener printListListener) {
        this(itemView);
        this.mPrintListListener = printListListener;
    }

    private void setListeners() {
        itemView.setOnClickListener(v -> {
            if (null != mPrintListListener) {
                mPrintListListener.onPrintListItemClick(getAdapterPosition(), mNetPrintInfo == null ? mBlueToothPrintInfo : mNetPrintInfo);
            }
        });
        itemView.setOnLongClickListener(v -> {
            if (null != mPrintListListener) {
                mPrintListListener.onLongPrintListItemClick(getAdapterPosition(), mNetPrintInfo == null ? mBlueToothPrintInfo : mNetPrintInfo);
            }
            return true;
        });
    }

    private void initView() {
        ip = itemView.findViewById(R.id.ip);
    }

    @Override
    public void setLayoutInfo(BasePrintInfo info) {
        switch (info.type) {
            case BasePrintInfo.NET_PRINT:
                mNetPrintInfo = (NetPrintInfo) info;


                String text = mNetPrintInfo.ip + "(" + "端口号：" + mNetPrintInfo.port + ")";
                SpannableString spannableString = new SpannableString(text);

                spannableString.setSpan(new AbsoluteSizeSpan(DensityUtil.sp2px(mContext, 9)), 0, mNetPrintInfo.ip.length() + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#444444")), 0, mNetPrintInfo.ip.length() + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

                spannableString.setSpan(new AbsoluteSizeSpan(DensityUtil.sp2px(mContext, 8)), mNetPrintInfo.ip.length() + 1, mNetPrintInfo.ip.length() + 5 + (mNetPrintInfo.port + "").length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#AAAAAA")), mNetPrintInfo.ip.length() + 1, mNetPrintInfo.ip.length() + 5 + (mNetPrintInfo.port + "").length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

                spannableString.setSpan(new AbsoluteSizeSpan(DensityUtil.sp2px(mContext, 9)), mNetPrintInfo.ip.length() + 5 + (mNetPrintInfo.port + "").length(), mNetPrintInfo.ip.length() + 6 + (mNetPrintInfo.port + "").length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#444444")), mNetPrintInfo.ip.length() + 5 + (mNetPrintInfo.port + "").length(), mNetPrintInfo.ip.length() + 6 + (mNetPrintInfo.port + "").length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                ip.setText(spannableString);
                break;
            case BasePrintInfo.BLEU_TOOTH_PRINT:
                mBlueToothPrintInfo = (BlueToothPrintInfo) info;
                break;
        }
    }
}
